package com.sduept.i1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.client.ClientChannelContext;
import org.tio.client.ReconnConf;
import org.tio.client.TioClient;
import org.tio.client.TioClientConfig;
import org.tio.client.intf.TioClientHandler;
import org.tio.client.intf.TioClientListener;
import org.tio.core.Node;
import org.tio.core.Tio;

import com.sduept.i1.model.WeatherData;
import com.sduept.i1.model.MonitorPacket;
import com.sduept.i1.model.TowerTiltData;
import com.sduept.i1.parse.Encoder;

//@Component
public class I1Client {
	
	private static final Logger logger = LoggerFactory.getLogger(I1Client.class);
			
    private volatile static I1Client me = null;
    public static I1Client getInstance() {
        if(me == null) {
            synchronized (I1Server.class) {
                if(me == null) {
                    me = new I1Client();
                }
            }
        }
        return me;
    }
	public static Node serverNode;
	// handler, 包括编码、解码、消息处理
	public static TioClientHandler tioClientHandler = new I1ClientHandler();
	// 事件监听器，可以为null，但建议自己实现该接口，可以参考showcase了解些接口
	public static TioClientListener aioListener = null;
	// 断链后自动连接的，不想自动连接请设为null
	private static final ReconnConf reconnConf = new ReconnConf(5000L);
	// 一组连接共用的上下文对象
	public static TioClientConfig clientTioConfig = new TioClientConfig(tioClientHandler, aioListener, reconnConf);
	public static TioClient tioClient;
	public static ClientChannelContext clientChannelContext = null;

	public static void connect(String ip, int port) throws Exception {
		serverNode = new Node(ip, port);
		tioClient = new TioClient(clientTioConfig);
		clientChannelContext = tioClient.connect(serverNode);
	}

	public static boolean isConnected() {
		logger.info("clientChannelContext-{}",clientChannelContext);
		if(clientChannelContext!=null) {
			//return true;
			logger.info("clientChannelContext-closed-{}",clientChannelContext.isClosed);
			return !clientChannelContext.isClosed;
		}
		else {
			return false;
		}
	}
	
	// 发送微气象数据
	public static void sendWeatherData() throws Exception {
		String deviceId = "Device1234567890a";
		WeatherData wd = new WeatherData();
		float f = 1.1F;
		for(short i=0;i<1;i++) {
		    wd.setComponentId("Device001");
		    wd.setTimeStamp(100);
		    wd.setAverageWindSpeed10min(f);
		    wd.setAverageWindDirection10min(i);
		    wd.setMaxWindSpeed(f);
		    wd.setExtremeWindSpeed(f);
		    wd.setStandardWindSpeed(f);
		    wd.setAirTemperature(f);
		    wd.setHumidity(i);
		    wd.setAirPressure(f);
		    wd.setPrecipitation(f);
		    wd.setPrecipitationIntensity(f);
		    wd.setRadiationIntensity(i);
			Thread.sleep(1000);
		}
		MonitorPacket packet = Encoder.makeMiniMeteorPacket(deviceId,wd);
		Tio.send(clientChannelContext, packet);
	}
	
	// 发送杆塔倾斜数据
	public static void sendTowerTiltData() {
		String deviceId = "Device1234567890a";
		TowerTiltData data = new TowerTiltData();
		float f = 1.1F;
		data.setComponentId("Device001");
		data.setTimeStamp(100);
		data.setInclination(f);
		data.setInclinationX(f);
		data.setInclinationY(f);
		data.setAngleX(f);
		data.setAngleY(f);
		MonitorPacket packet = Encoder.makeTowerTiltPacket(deviceId, data);
		Tio.send(clientChannelContext, packet);
	}
	
	public static void sendWorkStateData() {
		String deviceId = "device1234567890a";
		MonitorPacket packet = Encoder.makeWorkStatePacket(deviceId);
		Tio.send(clientChannelContext, packet);
	}
	
	// 发送监测装置基本信息数据
	public static void sendMonitorDeviceInfo() {
		String deviceId = "Device1234567890a";
		
		MonitorPacket packet = Encoder.makeAdditionalBasicInfoPacket(deviceId);
		Tio.send(clientChannelContext, packet);
	}
	
	// 发送监测装置工作状态数据
	public static void sendMonitorDeviceStatus() {
		String deviceId = "Device1234567890a";
		
		MonitorPacket packet = Encoder.makeAdditionalWorkStatePacket(deviceId);
		Tio.send(clientChannelContext, packet);
	}

	public static void close() {
		tioClient.stop();
		System.out.println("客户端已经关闭");
	}
	// 
	public static void main(String[] args) {
		try {
			I1Client.connect("192.168.2.156", 5002);
			I1Client.sendWorkStateData();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
